const app = getApp();
const { $util } = getApp();
Page({
  data: {
    avatarData: {
      imgSrc: '',
      size: 'large',
    },
    isSign: false,
    isLeave: false,
    isUpload: false,
    imgs: [], // 上传的图片列表
    isLoaded: false,
    recordData: {
      content: '',
      imgs: [],
    },
    showPropup: false,
    requestLoad: true, // 请求loading
    mode: '', // 1 表示从我的组织页面进入
    leave_reason: '', // 请假原因
    leavePopper: false, // 请假弹出层
    isRecorder: false, // 判断当前党员是否为记录员
    isParticipants: false, // 判断记录员是否为参会人员
    abnorData: {
      imgSrc: '/common/static/images/abnor-16.png',
      text: '没有会议记录',
    },
    imgUrl: $util.getImgUrl(),
    ifShowUpload:''
  },
  // 获取党员信息 id
  getSelfInfo(recorderid) {
    $util.request({
      url: 'entry/wxapp/partyman.mine.partymaninfo',
      data: {},
      showLoading: false,
      success: (res) => {
        if (res.data.inid == recorderid) { // 当前党员是记录员
          this.setData({
            isRecorder: true,
            recoder: res.data.realname,
          });
        }
      },
    });
  },
  getInfo() {
    this.loadDetail(this.data.id);
    this.checkRecording(this.data.id);
  },
  move: function() {},
  onLoad(opts) {
    this.setData(opts);
    this.loadDetail(opts.id);
    this.checkRecording(opts.id);
    this.initScope();
    wx.setNavigationBarTitle({
      //title: opts.barTitle && decodeURIComponent(opts.barTitle) || '会议详情',
      title:'会议详情'
    })
  },
  hideLeavePop() {
    this.setData({
      leavePopper: false,
    });
  },
  showLeavePop() {
    this.setData({
      leavePopper: true,
    });
  },
  initScope() {
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success: () => {
              this.getLocation();
            },
          });
        } else {
          this.getLocation();
        }
      },
    });
  },
  getLocation(e) {
    let flag = '';
    if (e) {
      flag = e.currentTarget.dataset.flag;
    }
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        this.setData({
          lat: res.latitude,
          lng: res.longitude,
        }, () => {
          if (flag === 'sign') {
            this.sign();
          }
        });
      },
      fail: () => {
        this.showPropup();
      },
    });
  },
  /**
   * 获取会议详情  mode=1 从我的组织进入
   */
  loadDetail(id) {
    let that = this;
    $util.request({
      url: that.data.mode === '1' ? 'entry/wxapp/partylife.meets.getmeetsdetail' : 'entry/wxapp/partylife.sessions.getmeetcontent',
      data: {
        id,
      },
      showLoading: that.data.requestLoad,
      success: (res) => {
        const detail = res.data;
        const markers = [{
          latitude: detail.lat,
          longitude: detail.lng,
          name: detail.place,
          alpha: 0.5,
          width: '38rpx',
          height: '38rpx',
          iconPath: that.data.imgUrl + 'marker.png',
          callout: {
            content: detail.place,
            bgColor: '#fff',
            display: 'ALWAYS',
          },
          anchor: {
            x: 0.5,
            y: 0.3,
          },
        }];
        const circles = [{
          latitude: detail.lat,
          longitude: detail.lng,
          color: '#B81C24',
          fillColor: '#ff900028',
          radius: Number(detail.signdistance),
          strokeWidth: 0,
        }];
        // wx.setNavigationBarTitle({
        //   title: detail.title,
        // });
        // sign  0-未签到 1-已签到 2-请假
        that.setData({
          markers,
          detail,
          circles,
          isSign: Number(detail.sign) === 1 ? true : false,
          isUpload: Number(detail.upload),
          isLeave: Number(detail.sign) === 2 ? true : false,
          isLoaded: true,
          'avatarData.imgSrc': detail.thumb,
          isParticipants: detail.is_participants,
          recordImgs: detail.recordimg,
          recordTxt: detail.recorddetail,
          recordDetail: detail.recorddetail,
          recordPic: detail.recordpic,
        });

        // 判断当前 党员id 和 记录员id 是否相同
        if (that.data.mode === '') { // 不是从我的组织进入的
          that.getSelfInfo(res.data.recorderid);
        }
        
      },
      fail: (res) => {
        console.log('fail:', res);
        if (res.errno === 41009) {
          $util.getUserInfo(() => {
            $util.request(option);
          });
          return;
        } else if (res.errno === -1) {
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 2000,
            success: () => {
              setTimeout(() => {
                wx.switchTab({
                  url: '/ws_party_const/pages/index/index',
                });
              }, 1800);
            },
          });
        } else if (res.errno === -99) {
          wx.showModal({
            title: '提示',
            content: '请先认证党员信息',
            showCancel: false,
            success: (res) => {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/ws_party_const/pages/auth/index',
                });
              }
            },
          });
        } else if (res.message) {
          let redirect = '';
          if (res.data != null && res.data.redirect) {
            redirect = res.data.redirect;
          } else {
            redirect = '';
          }
          $util.message(res.message, redirect, 'error');
        }
      },
    });
  },
  /**
   * 签到
   */
  sign() {
    const {
      lat,
      lng,
      id,
    } = this.data;
    $util.request({
      url: 'entry/wxapp/partylife.sessions.setmeetsign',
      data: {
        id,
        lat,
        lng,
      },
      success: () => {
        wx.showToast({
          title: '签到成功',
          icon: 'success',
          mask: true,
          duration: 2e3,
          success: () => {
            this.setData({
              isSign: true,
              requestLoad: false,
            });
            setTimeout(() => {
              this.loadDetail(id);
            }, 1500);
          },
        });
      },
    });
  },
  bindIpVal(e) {
    this.setData({
      leave_reason: e.detail.value,
    });
  },
  // 请假
  leave() {
    const {
      id,
      leave_reason,
    } = this.data;
  
    if (leave_reason.replace(/^ +| +$/g, '') == '') {
      wx.showToast({
        title: '请假原因不能为空',
        icon: 'none',
      });
      return;
    }

    $util.request({
      url: 'entry/wxapp/partylife.sessions.setleave',
      data: {
        id,
        leave_reason,
      },
      success: () => {
        wx.showToast({
          title: '请假成功',
          icon: 'success',
          mask: true,
          duration: 2e3,
          success: () => {
            this.setData({
              isLeave: true,
              requestLoad: false,
              leavePopper: false,
            });
            setTimeout(() => {
              this.loadDetail(id);
            }, 1500);
          },
        });
      },
    });
  
  },
  /**
   * 上传(会议记录)?
   */
  upload() {
    let { id } = this.data;
    app.util.navigateTo('/ws_party_const/pages/meeting-record/meeting-record', {
      id,
    });
  },
  showRecoder() {
    let { id, isRecorder, recoder } = this.data;
    app.util.navigateTo('/ws_party_const/pages/meeting-record/meeting-record', {
      id,
      isRecorder,
      recoder,
    });
  },
  /**
   * 查看记录
   */
  checkRecording(id) {
    $util.request({
      url: 'entry/wxapp/partylife.sessions.getmeetrecord',
      data: {
        id,
      },
      success: (res) => {
        this.setData({
          'recordData.content': res.data.record,
          'recordData.imgs': res.data.image,
        });
        console.log(res.data.if_upload_record)
        if(res.data.if_upload_record == 0){
           this.setData({
            ifShowUpload:false
           })
        }else if(res.data.if_upload_record == 1){
          this.setData({
            ifShowUpload:true
          })
        }
      },
    });
  },

  /**
   * 显示弹出框
   */
  showPropup() {
    this.setData({
      showPropup: true,
      propup: {
        mask: true,
        confirmText: '确认',
        cancelText: '取消',
        title: '温馨提示',
        content: '为了给您提供更好的服务，请授权您的地理位置信息',
        showCancel: false,
      },
    });
  },
  /**
   * 隐藏弹出框
   */
  hidePropup() {
    this.setData({
      showPropup: false,
    });
  },
  /**
   * 点击确认按钮
   */
  confirm() {
    this.hidePropup();
  },
  /**
   * 点击取消按钮
   */
  cancel() {
    this.hidePropup();
  },
  /**
   * 阻止默认事件
   */
  preventDefault() {
    return false;
  },
  // 导航
  navigation() {
    const {
      detail,
    } = this.data;
    wx.authorize({
      scope: 'scope.userLocation',
      success: () => {
        wx.openLocation({
          latitude: Number(detail.lat),
          longitude: Number(detail.lng),
          name: detail.title,
          address: detail.place,
          scale: 18,
        });
      },
      fail: () => {
        this.showPropup();
      },
    });
  },
  vote() {
    let that = this;
    $util.request({
      url: 'entry/wxapp/partyman.vote.getVoteOption',
      data: {
        voteid: that.data.detail.voteid,
      },
      success: (res) => {
        if (Number(res.errno) === 0) {
          app.util.navigateTo('/ws_party_const/pages/vote/detail', {
            id: that.data.detail.voteid,
          });
        } else {
          wx.showToast({
            title: res.message,
            icon: 'none',
          });
        }
      },
    });
  },
});